package qjc.z_排序;

import java.util.Arrays;

public class z4_插入排序 {
    /**
     * 分为左右两部分[0,low-1],[low,nums.length-1]，
     * 其中，[0,low-1]是已排好的区域
     *
     * @param nums
     */
    private static void sort(int[] nums) {
        for (int low = 1; low < nums.length; low++) {
            int t = nums[low];
            int i = low - 1;
            while (i >= 0 && t < nums[i]) {
                nums[i + 1] = nums[i];
                i--;
            }
            if (i != low - 1) {
                nums[i + 1] = t;
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {9, 3, 7, 2, 5, 8, 1, 4};
        System.out.println(Arrays.toString(a));
        sort(a);
        System.out.println(Arrays.toString(a));
    }

}
